﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TravelLink.WebModel;
using Interactive.Utility.Extension;
using DevExpress.Web.ASPxGridView;
using TravelLink.WebApp.UserControl.Member.JobEditable;
using TravelLink.DAL;


namespace TravelLink.WebApp.Admin {
    public partial class AdminResumeModify_EN : Pagebase {
        private int _ResumeId;


        Dictionary<string, ASPxGridView> _gridViewDict;


        JobEditableFacade facade;


        public string UserPhoto = string.Empty;

        public string BirthDay = string.Empty;

        public bool HasPhoto {
            get {
                return !string.IsNullOrEmpty(UserPhoto);
            }
        }


        private int EditResumeId {
            get {

                return WebHelper.GetQueryStringInt("idx", 0);
            }
        }


        private TL_Resume EditResume {
            get {
                if (EditResumeId > 0) {
                    return this.DB.TL_Resume.FirstOrDefault(u => u.Idx == EditResumeId);
                }
                return null;
            }
        }

        protected void Page_Load(object sender, EventArgs e) {

            TL_Admin opr = Session["Admin"] as TL_Admin;
            if (opr == null) {
                Response.Redirect("AdminLogin.aspx");
                return;
            }


            _ResumeId = EditResumeId;

            if (_ResumeId <= 0) {
                this.Visible = false;
                return;
            }



            _gridViewDict = new Dictionary<string, ASPxGridView>(7);

            _gridViewDict.Add(aspGridSkills.ID, aspGridSkills);
            _gridViewDict.Add(aspGridEducation.ID, aspGridEducation);
            _gridViewDict.Add(this.aspxGridMembership.ID, aspxGridMembership);
            _gridViewDict.Add(aspxGridAward.ID, aspxGridAward);
            _gridViewDict.Add(aspxGridQualification.ID, aspxGridQualification);
            _gridViewDict.Add(aspxGridCareer.ID, aspxGridCareer);

            // _ResumeId = 1;

            facade = new JobEditableFacade(_gridViewDict, false, _ResumeId, EditResume.UserCode.ToString());

            facade.Initialize();



            if (!this.IsPostBack) {
                InitializeDropdown();

                TL_User user = this.DB.TL_User.FirstOrDefault(u => u.UserCode == EditResume.UserCode);

                TL_Resume resume = EditResume;
                UserPhoto = user.PhotoImage;
                this.txtAddress.Text = resume.Address;
                BirthDay = user.BirthDay.HasValue ? user.BirthDay.Value.ToString("yyyy/MM/dd") : string.Empty;

                this.txtBirthDay.Text = user.BirthDay.HasValue ? user.BirthDay.Value.ToString("yyyy-MM-dd") : string.Empty;
                this.txtCity.Text = resume.ExpectedWorkin;
                this.txtCityDisplay.InnerHtml = resume.ExpectedWorkin;
                this.txtEmail.Text = user.Email;
                this.txtEnName.Text = (user.EFirstName ?? string.Empty) + " " + (user.ELastName ?? string.Empty);
                this.txtInterests.Text = resume.Interests;
                this.txtInterestsDisplay.InnerHtml = (resume.Interests ?? string.Empty).Replace("\n", "<br>");
                this.txtLanguageDesc.Text = resume.LanguageDesc;
                this.txtLanguageDescDisplay.InnerHtml = (resume.LanguageDesc ?? string.Empty).Replace("\n", "<br>");
                this.txtMobile.Text = resume.Mobile ?? user.Mobile;
                this.txtName.Text = (user.CFirstName ?? string.Empty) + " " + (user.CLastName ?? string.Empty);

                this.txtObjective.Text = resume.Objective;
                this.txtObjectiveDisplay.InnerHtml = (resume.Objective ?? string.Empty).Replace("\n", "<br>");
                this.txtRemark.Text = resume.Profile;
                this.txtTel.Text = user.Tel;
                this.txtZipcode.Text = user.PostCode;

                if (user.Gender.HasValue) {

                    this.chkGender_1.Checked = user.Gender ?? false;
                    this.chkGender_0.Checked = !this.chkGender_1.Checked;
                }
                if (user.IsMarried.HasValue) {

                    this.chkMarry_0.Checked = user.IsMarried ?? false;
                    this.chkMarry_1.Checked = !this.chkMarry_0.Checked;
                }

                if (user.TL_Country != null) {
                    this.dropCountry.SelectedValue = user.TL_Country.ID.ToString();
                }
            }


        }

        private void InitializeDropdown() {

            this.dropCountry.DataSource = this.DB.TL_Country.ToList();
            this.dropCountry.DataTextField = "ECountry";
            this.dropCountry.DataValueField = "ID";
            this.dropCountry.DataBind();
            dropCountry.Items.Insert(0, new ListItem("Choose Country", "-1"));

        }

        #region Grid Event

        protected void RowDeleted(object sender, DevExpress.Web.Data.ASPxDataDeletedEventArgs e) {
            e.ExceptionHandled = true;
        }

        protected void RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e) {
            facade.RowDeleting(sender, e);
        }

        protected void RowUpdated(object sender, DevExpress.Web.Data.ASPxDataUpdatedEventArgs e) {
            e.ExceptionHandled = true;

        }

        protected void RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e) {
            facade.RowUpdating(sender, e);

        }

        protected void RowInserted(object sender, DevExpress.Web.Data.ASPxDataInsertedEventArgs e) {
            e.ExceptionHandled = true;
        }

        protected void RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e) {

            facade.RowInserting(sender, e);
        }


        protected void aspGridSkills_RowDeleted(object sender, DevExpress.Web.Data.ASPxDataDeletedEventArgs e) {
            RowDeleted(sender, e);
        }

        protected void aspGridSkills_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e) {
            RowDeleting(sender, e);
        }

        protected void aspGridSkills_RowInserted(object sender, DevExpress.Web.Data.ASPxDataInsertedEventArgs e) {
            RowInserted(sender, e);
        }

        protected void aspGridSkills_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e) {
            RowInserting(sender, e);
        }

        protected void aspGridSkills_RowUpdated(object sender, DevExpress.Web.Data.ASPxDataUpdatedEventArgs e) {
            RowUpdated(sender, e);
        }

        protected void aspGridSkills_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e) {
            RowUpdating(sender, e);
        }

        #endregion

        protected void btnSubmit_Click(object sender, ImageClickEventArgs e) {

            string photos = WebHelper.GetFormStr("imgFileValues");
            TL_Resume resume = this.DB.TL_Resume.FirstOrDefault(u => u.Idx == _ResumeId && u.UserCode == EditResume.UserCode);
            TL_User user = this.DB.TL_User.FirstOrDefault(u => u.UserCode == EditResume.UserCode);

            resume.Address = this.txtAddress.Text;
            string dt = this.txtBirthDay.Text;
            if (dt != "") {

                user.BirthDay = Convert.ToDateTime(dt);
            }
            else {
                user.BirthDay = null;
            }

            resume.ExpectedWorkin = this.txtCity.Text;
            user.Email = this.txtEmail.Text;
            user.EFirstName = this.txtEnName.Text;
            resume.Interests = this.txtInterests.Text;
            resume.LanguageDesc = this.txtLanguageDesc.Text;
            resume.Mobile = this.txtMobile.Text;
            user.CFirstName = this.txtName.Text;
            user.PhotoImage = photos;
            resume.Objective = this.txtObjective.Text;
            resume.Profile = this.txtRemark.Text;
            user.Tel = this.txtTel.Text;
            user.PostCode = this.txtZipcode.Text;
            user.Gender = this.chkGender_1.Checked;
            user.IsMarried = this.chkMarry_0.Checked;
            user.CountryId = this.dropCountry.SelectedValue.AsInt();
            resume.RowTime = DateTime.Now;
            this.DB.SubmitChanges();
            facade.Save(resume);
            UserPhoto = photos;
            Response.Redirect("AdminResumeModify_CN.aspx?idx=" + _ResumeId);
        }
    }

}